Performance-critical optimization - Define benchmarks for your most important functions and let Codeflash measure the real-world impact of every optimization on your performance metrics.
Using Codeflash in Benchmark Mode
-
Create a benchmarks root
Create a directory for benchmarks if it does not already exist.
In your pyproject.toml, add the path to the ‘benchmarks-root’ section.
-
Define your benchmarks
Currently, Codeflash only supports benchmarks written as pytest-benchmarks. Check out the pytest-benchmark documentation for more information on syntax.
For example:
Note that these benchmarks should be defined in such a way that they don’t take a long time to run. The pytest-benchmark format is simply used as an interface. The plugin is actually not used - Codeflash will run these benchmarks with its own pytest plugin
-
Run Codeflash
Run Codeflash with the
--benchmarkflag. Note that benchmark mode cannot be used with--all.If you did not define your benchmarks-root in your pyproject.toml, you can do: -
Run Codeflash in CI
Benchmark mode is best used together with Codeflash as a Github Action. This way, with every PR, you will know the impact of Codeflash’s optimizations on your benchmarks.
Use
codeflash initfor an easy way to set up Codeflash as a Github Action (with the option to enable benchmark mode). Otherwise, you can run the following command in your Codeflash GitHub Action:
How it works
- Codeflash identifies benchmarks in the benchmarks-root directory.
- The benchmarks are run so that runtime statistics and information can be recorded.
- Replay tests are generated so the performance of optimization candidates on the exact inputs used in the benchmarks can be measured.
- If an optimization candidate is verified to be correct, the speedup of the optimization is calculated for each benchmark.
- Codeflash then reports the impact of the optimization on each benchmark.